﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AmuTools;
using app.Api;

namespace app.Models
{
    // 投资项目表
    [Model(TableName = "t_investment", PrimaryKey = "id", IdentityInsert = false)]
    public class Investment
    {
        /*
         *标题
         * 贷款对象
         * 申报条件
         * 贷款依据
         * 产品特色
         * 产品流程
         * 贷款金额
         * 贷款期限
         * 
         * 机构名称
         * 办理部门
         * 联系人
         * 联系电话
         * 
         * 附件
         * 备注
        */

        public int id { get; set; }

        public string title { get; set; }

        [Field(Webable = false)]
        public string member_id { get; set; }

        private Member _member = null;
        [Field(Storageable = false)]
        public Member member
        {
            get
            {
                if (_member == null)
                {
                    _member = G.DB.GetById<Member>(this.member_id);
                }
                return _member;
            }
            set
            {
                _member = value;
                member_id = _member.id.ToString();
            }
        }

        [Field(DataType = "char(13)")]
        public string create_time { get; set; }

        public string capital_type { get; set; }

        public string area { get; set; }

        public string investment_industry { get; set; }

        public string investment_area { get; set; }

        public int amount_min { get; set; }

        public int amount_max { get; set; }

        public int term_min { get; set; }

        public int term_max { get; set; }

        public string investment_type { get; set; }

        public string needed_appendix { get; set; }

        [Field(DataType = "nvarchar(500)")]
        public string requirement { get; set; }

        public string remark { get; set; }
        
        [Field(DataType = "text")]
        public string thumbnail { get; set; }
        
        private string _appendix_ids = "";
        public string appendix_ids {
            get { return _appendix_ids; }
            set
            {
                _appendix_ids = value;
                _appendix = null;
            }
        }

        private List<UploadedFile> _appendix = null;
        [Field(Storageable = false)]
        public List<UploadedFile> appendix {
            get
            {
                if(_appendix == null)
                {
                    if (appendix_ids == "") _appendix = new List<UploadedFile>();
                    else _appendix = G.DB.Get<UploadedFile>(string.Format("{0} in ({1})", G.DB.GetPrimaryKey<UploadedFile>(), appendix_ids)).GetFirstTableList<UploadedFile>();
                }
                return _appendix;
            }
        }

        public string investment_stage { get; set; }

        [Field(DataType = "numeric(8,5)")]
        public double min_return { get; set; }

        public int status { get; set; }

        public string failed_reason { get; set; }

        [Field(Webable = false)]
        public int is_delete { get; set; }

        public static Investment[] GetInitData()
        {
            return new Investment[]{
                new Investment { id = 1, title = "五个亿，干大事儿" }
            };
        }
    }
    
}